/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.oracleandsxnd.erp.member.service;

import com.oracleandsxnd.erp.common.persistence.Page;
import com.oracleandsxnd.erp.common.service.CrudService;
import com.oracleandsxnd.erp.common.service.ServiceException;
import com.oracleandsxnd.erp.member.dao.MemberIntegralGiveDao;
import com.oracleandsxnd.erp.member.entity.Member;
import com.oracleandsxnd.erp.member.entity.MemberIntegralGive;
import com.oracleandsxnd.erp.member.entity.MemberIntegralLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员积分赠送管理Service
 * @author 会员积分赠送管理
 * @version 2019-12-13
 */
@Service
@Transactional(readOnly = true)
public class MemberIntegralGiveService extends CrudService<MemberIntegralGiveDao, MemberIntegralGive> {

	public MemberIntegralGive get(String id) {
		return super.get(id);
	}
	
	public List<MemberIntegralGive> findList(MemberIntegralGive memberIntegralGive) {
		return super.findList(memberIntegralGive);
	}
	
	public Page<MemberIntegralGive> findPage(Page<MemberIntegralGive> page, MemberIntegralGive memberIntegralGive) {
		return super.findPage(page, memberIntegralGive);
	}
	@Autowired
	private MemberService memberService;
	@Autowired
	private MemberIntegralLogService memberIntegralLogService;
	@Transactional(readOnly = false)
	public void save(MemberIntegralGive memberIntegralGive) {
		Member m=memberService.get(memberIntegralGive.getMemberid());
		int integral= m.getIntegral();
		int lintegral=0;
		if(memberIntegralGive.getType().equals("1")){
			lintegral=integral+memberIntegralGive.getIntegral();
		}
        else{
			lintegral=integral-memberIntegralGive.getIntegral();
		}
		if(lintegral<0){
			throw new ServiceException("积分不够");
		}
		m.setIntegral(lintegral);
		memberService.save(m);
		MemberIntegralLog memberIntegralLog=new MemberIntegralLog();
		memberIntegralLog.setMemberid(m.getId());
		memberIntegralLog.setMembername(m.getName());
		memberIntegralLog.setIntegral(memberIntegralGive.getIntegral());
		memberIntegralLog.setLastintegral(lintegral);
		memberIntegralLogService.save(memberIntegralLog);
		memberIntegralGive.setMembername(m.getName());
		super.save(memberIntegralGive);
	}
	
	@Transactional(readOnly = false)
	public void delete(MemberIntegralGive memberIntegralGive) {
		super.delete(memberIntegralGive);
	}
	
}